package com.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.reggie.common.R;
import com.reggie.dto.OrdersDto;
import com.reggie.entity.Orders;
import com.reggie.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders) {
        orderService.submitOrder(orders);
        return R.success("付款成功");
    }

    @GetMapping("userPage")
    public R<Page<OrdersDto>> userPage(Integer page, Integer pageSize) {
        Page<OrdersDto> ordersDtoPage = orderService.userPage(page, pageSize);
        return R.success(ordersDtoPage);
    }

    /**
     * 条件查询+分页
     * @param page
     * @param pageSize
     * @param number
     * @param beginTime
     * @param endTime
     * @return
     */
    @GetMapping("/page")
    public R<Page<OrdersDto>> page(Integer page, Integer pageSize, Long number, @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime beginTime, @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime) {
        Page<OrdersDto> ordersDtoPage = orderService.pageOrdersDto(page, pageSize, number, beginTime, endTime);
        return R.success(ordersDtoPage);
    }

    @PutMapping
    public R<String> updateStatus(@RequestBody Orders orders) {
        orderService.updateById(orders);
        return R.success("修改成功");
    }
    @PostMapping("/again")
    public R<String> again(@RequestBody Orders orders){
        orderService.againOrder(orders.getId());
        return R.success("下单成功");
    }
}
